#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
**=============================================================
 * Copyright: 2012~2015 NSN
 * FullName: tddci.utils.sqlbase.simple_table_operate
 * Description: 
 * Changes: 
 *==============================================================
 * Date: Apr 22, 2014
 * Author: y21chen(yong-cy.chen@nsn.com)          
 * Comment:  Create file, implement feature    
**==============================================================
"""

from base_action import BaseSqlAction
from dict_tosql import SimpleTBDictToSql


class SimpleTableSql(object):
    '''
        Simple table class init with table name.
        input condtion you can get Sql string
    '''
    def __init__(self,table):
        self.table_object = SimpleTBDictToSql(table)
        
    def get_query(self,condition):
        return self.table_object.get_query(condition)
    
    def get_insert_one(self,values):
        return self.table_object.get_insert_one(values)
    
    def get_update(self,values,condition):
        return self.table_object.get_update(values, condition)
    
    def get_delete(self,condition):
        return self.table_object.get_delete(condition)

class SimpleTBAction(object):
    '''
        execute some simple sql and get the result
    '''
    def __init__(self,table):
        self.simple_table = SimpleTableSql(table)
    
    def query(self,condition):
        return BaseSqlAction().query(self.simple_table.get_query(condition))
    
    def add(self,values):
        return BaseSqlAction().add(self.simple_table.get_insert_one(values))
    
    def update(self,values,condition):
        return BaseSqlAction().update(self.simple_table.get_update(values, condition))
    
    def delete(self,condition):
        return BaseSqlAction().delete(self.simple_table.get_delete(condition))
    
class SimpleTBDo(object):
    '''
       init with table name
    '''
    def __init__(self,table):
        self.table = SimpleTBAction(table)

    def is_exist(self,condition):
        '''
            check if is exist
        eg:
        res = self.table.query('baseline="%s"' % baseline)
        print(res)
        if res == '' or res == None or len(res) == 0:
            return False
        else:
            return True
        '''
        pass
    
    def add_or_update(self,data):
        '''
            Add or update the data
        eg:
        if data.get('baseline'):
            if self.is_exist(data['baseline']):
                res = self.table.update(data, 'baseline="%s"' % data['baseline'])
                print(res)
            else:
                res = self.table.add(data)
        '''
        pass
        
    
    def parse_arguments(self):
        '''
            parse the argument convert to table field
        eg:
        parser = argparse.ArgumentParser(description='This is a update table script')
        parser.add_argument('-baseline',action='store', dest='baseline', help='package baseline')
        parser.add_argument('-compile', action='store', dest='compile', help='package compile result')
        parser.add_argument('-qt1', action='store', dest='qt1', help='qt1 status')
        parser.add_argument('-qt2', action='store', dest='qt2', help='qt2 status')
        parser.add_argument('-wft_flag', action='store', dest='wft_flag', help='wft_flag')
        parser.add_argument('-release_date', action='store', dest='release_date', help='package release date')
        parser.add_argument('-release_time', action='store', dest='release_time', help='package release time')
        parser.add_argument('-branch', action='store', dest='branch', help="package's branch")
        parser.add_argument('-release', action='store', dest='release', help='release status')
        parser.add_argument('-rollback', action='store', dest='rollback', help='rollback market')
        parser.add_argument('-rollback_id', action='store', dest='rollback_id', help='refer rollback info id')
        parser.add_argument('-status', action='store', dest='status', help='trunk status green or red')
        parser.add_argument('-score', action='store', dest='score', help='qt score')
        args = parser.parse_args()
        return args
        '''
        pass
    
    def args_todict(self,args):
        '''
            Covert args to table dict
        eg:
        data = {}
        if args.baseline:
            data['baseline'] = args.baseline
        if args.compile:
            data['compile'] = args.compile
        if args.qt1:
            data['qt1'] = args.qt1
        if args.qt2:
            data['qt2'] = args.qt2
        if args.wft_flag:
            data['wft_flag'] = args.wft_flag
        if args.release_date:
            data['release_date'] = args.release_date
        if args.release_time:
            data['release_time'] = args.release_time
        if args.branch:
            data['branch'] = args.branch
        if args.release:
            data['release'] = args.release
        if args.rollback:
            data['rollback'] = args.rollback
        if args.rollback_id:
            data['rollback_id'] = args.rollback_id
        if args.status:
            data['status'] = args.status
        if args.score:
            data['score'] = args.score
        return data
        '''
        pass
